package com.css.fxfzaqbz.modules.assess1.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.basedata.entity.*;
import com.css.fxfzaqbz.constants.FxfzConstants;
import com.css.fxfzaqbz.modules.assess1.entity.*;
import com.css.fxfzaqbz.modules.assess1.queryParams.DestoryRateQueryParams;
import com.css.fxfzaqbz.modules.assess1.repository.DestoryAlgRepository;
import com.css.fxfzaqbz.modules.assess1.service.CountPopulationService;
import com.css.fxfzaqbz.modules.assess1.service.DestoryAlgService;
import com.css.fxfzaqbz.modules.assess1.service.DestoryRateService;
import com.css.fxfzaqbz.modules.base.entity.HouseData;
import com.css.fxfzaqbz.modules.supergraph.repository.OrgDivisionRepository;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import com.css.fxfzaqbz.util.RestTemplateUtil;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.slw.common.helper.PropsHelper;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author y1fan
 * @create 2022-12-01 14:48
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DestoryAlgServiceImpl implements DestoryAlgService {


    @Resource
    private RestTemplateUtil restTemplateUtil;

    @Resource
    private DestoryRateService destoryRateService;

    @Resource
    private DestoryAlgRepository destoryAlgRepository;

    @Resource
    private CountPopulationService countPopulationService;

    @Resource
    private OrgDivisionRepository orgDivisionRepository;


    Properties configGroup = PropsHelper.loadProps("slw-config.properties");
    String poUrl = configGroup.getProperty("restTemplateUrl") + "/datacenter/house/getPopu";

    @Override
    public RestResponse alg(AlgVO algVO) {
 /*       DistrictEntity districtEntity1=new DistrictEntity();
        districtEntity1.setProvince("北京市");
        districtEntity1.setCity("北京市");
        districtEntity1.setPrefecture("东城区");
        ArrayList<DistrictEntity> objects = new ArrayList<>();
        objects.add(districtEntity1);
        algVO.setList(objects);*/
        //保存编号：SW+时间到秒(KZ20230719103637)
        Date date = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUIDGenerator.getUUID();

        List<MedianEntity> medianEntities = new ArrayList<>();
        List<DistrictEntity> list = algVO.getList();
        for (DistrictEntity districtEntity : list) {
            //删除原有的省市区人员伤亡数据
          //  destoryAlgRepository.delete(districtEntity, algVO.getIaId());
            //根据省市区查询基础数据5张表
            Properties configGroup = PropsHelper.loadProps("slw-config.properties");
            String url = configGroup.getProperty("restTemplateUrl") + "/datacenter/house/getHouseData";
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("province", districtEntity.getProvince());
            paramMap.put("city", districtEntity.getCity());
            paramMap.put("county", districtEntity.getPrefecture());


            paramMap.put("czzzDataVersion", algVO.getCzzzDataVersion());
            paramMap.put("czfzzDataVersion", algVO.getCzfzzDataVersion());
            paramMap.put("nczzdlzzDataVersion", algVO.getNczzdlzzDataVersion());
            paramMap.put("nczzjtzzDataVersion", algVO.getNczzjtzzDataVersion());
            paramMap.put("ncfzzDataVersion", algVO.getNcfzzDataVersion());
            ResponseEntity<String> cchResponseEntity = restTemplateUtil.sendPostHttpRequest1(url, paramMap);
            List<HouseData> houses = JSONObject.parseArray(cchResponseEntity.getBody(), HouseData.class);


            if (houses.size() == 0) {
                return RestResponse.fail("业务数据中未查到房屋数据，请添加房屋数据");
            }

            //所在省市区每种结构类型的面积
            BigDecimal zeroArea = new BigDecimal(0);
            BigDecimal oneArea = new BigDecimal(0);
            BigDecimal twoArea = new BigDecimal(0);
            BigDecimal threeArea = new BigDecimal(0);
            BigDecimal fourArea = new BigDecimal(0);
            BigDecimal fiveArea = new BigDecimal(0);
            BigDecimal sixArea = new BigDecimal(0);
            BigDecimal sevenArea = new BigDecimal(0);
            BigDecimal eightArea = new BigDecimal(0);
            BigDecimal nineArea = new BigDecimal(0);

            for (HouseData house : houses) {
                if (house.getJglx().equals(FxfzConstants.TUMU_STRUCTURE)) {
                    zeroArea = zeroArea.add(house.getDcmj()).setScale(2, RoundingMode.HALF_UP);
                }
                if (house.getJglx().equals(FxfzConstants.QITI_STRUCTURE)) {
                    oneArea = oneArea.add(house.getDcmj()).setScale(2, RoundingMode.HALF_UP);
                }
                if (house.getJglx().equals(FxfzConstants.MU_STRUCTURE)) {
                    twoArea = twoArea.add(house.getDcmj()).setScale(2, RoundingMode.HALF_UP);
                }
                if (house.getJglx().equals(FxfzConstants.YAOT_STRUCTURE)) {
                    threeArea = threeArea.add(house.getDcmj()).setScale(2, RoundingMode.HALF_UP);
                }
                if (house.getJglx().equals(FxfzConstants.GJHNT_STRUCTURE)) {
                    fourArea = fourArea.add(house.getDcmj()).setScale(2, RoundingMode.HALF_UP);
                }
                if (house.getJglx().equals(FxfzConstants.MUZHU_STRUCTURE)) {
                    fiveArea = fiveArea.add(house.getDcmj()).setScale(2, RoundingMode.HALF_UP);
                }
                if (house.getJglx().equals(FxfzConstants.HZ_STRUCTURE)) {
                    sixArea = sixArea.add(house.getDcmj()).setScale(2, RoundingMode.HALF_UP);
                }
                if (house.getJglx().equals(FxfzConstants.GANG_STRUCTURE)) {
                    sevenArea = sevenArea.add(house.getDcmj()).setScale(2, RoundingMode.HALF_UP);
                }
                if (house.getJglx().equals(FxfzConstants.OTHER_STRUCTURE)) {
                    eightArea = eightArea.add(house.getDcmj()).setScale(2, RoundingMode.HALF_UP);
                }
                if (house.getJglx().equals(FxfzConstants.KZQQT_STRUCTURE)) {
                    nineArea = nineArea.add(house.getDcmj()).setScale(2, RoundingMode.HALF_UP);
                }
            }

            //查询该区下的破坏比例
            DestoryRateQueryParams destoryRateQueryParams = new DestoryRateQueryParams();
            destoryRateQueryParams.setProvince(districtEntity.getProvince());
            destoryRateQueryParams.setCity(districtEntity.getCity());
            destoryRateQueryParams.setPrefecture(districtEntity.getPrefecture());
            //各类型破坏比率实体
            List<DestoryRateEntity> rateList = destoryRateService.getPage(destoryRateQueryParams);
            //各类型的破坏面积实体   破坏面积=破坏比率*面积
            List<DestoryAreaEntity> destoryAreaEntityList = new ArrayList<>();

            for (DestoryRateEntity destoryRateEntity : rateList) {
                if (destoryRateEntity.getStructureType().equals(FxfzConstants.TUMU_STRUCTURE)) {
                    DestoryAreaEntity destoryAreaEntity = new DestoryAreaEntity();
                    destoryAreaEntity.setStructureType(FxfzConstants.TUMU_STRUCTURE);
                    destoryAreaEntity.setJbArea(destoryRateEntity.getZero().multiply(zeroArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setQwArea(destoryRateEntity.getOne().multiply(zeroArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setZdArea(destoryRateEntity.getTwo().multiply(zeroArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setYzArea(destoryRateEntity.getThree().multiply(zeroArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setHhArea(destoryRateEntity.getFour().multiply(zeroArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntityList.add(destoryAreaEntity);
                }

                if (destoryRateEntity.getStructureType().equals(FxfzConstants.QITI_STRUCTURE)) {
                    DestoryAreaEntity destoryAreaEntity = new DestoryAreaEntity();
                    destoryAreaEntity.setStructureType(FxfzConstants.QITI_STRUCTURE);
                    destoryAreaEntity.setJbArea(destoryRateEntity.getZero().multiply(oneArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setQwArea(destoryRateEntity.getOne().multiply(oneArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setZdArea(destoryRateEntity.getTwo().multiply(oneArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setYzArea(destoryRateEntity.getThree().multiply(oneArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setHhArea(destoryRateEntity.getFour().multiply(oneArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntityList.add(destoryAreaEntity);
                }

                if (destoryRateEntity.getStructureType().equals(FxfzConstants.MU_STRUCTURE)) {
                    DestoryAreaEntity destoryAreaEntity = new DestoryAreaEntity();
                    destoryAreaEntity.setStructureType(FxfzConstants.MU_STRUCTURE);
                    destoryAreaEntity.setJbArea(destoryRateEntity.getZero().multiply(twoArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setQwArea(destoryRateEntity.getOne().multiply(twoArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setZdArea(destoryRateEntity.getTwo().multiply(twoArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setYzArea(destoryRateEntity.getThree().multiply(twoArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setHhArea(destoryRateEntity.getFour().multiply(twoArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntityList.add(destoryAreaEntity);
                }

                if (destoryRateEntity.getStructureType().equals(FxfzConstants.YAOT_STRUCTURE)) {
                    DestoryAreaEntity destoryAreaEntity = new DestoryAreaEntity();
                    destoryAreaEntity.setStructureType(FxfzConstants.YAOT_STRUCTURE);
                    destoryAreaEntity.setJbArea(destoryRateEntity.getZero().multiply(threeArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setQwArea(destoryRateEntity.getOne().multiply(threeArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setZdArea(destoryRateEntity.getTwo().multiply(threeArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setYzArea(destoryRateEntity.getThree().multiply(threeArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setHhArea(destoryRateEntity.getFour().multiply(threeArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntityList.add(destoryAreaEntity);
                }

                if (destoryRateEntity.getStructureType().equals(FxfzConstants.GJHNT_STRUCTURE)) {
                    DestoryAreaEntity destoryAreaEntity = new DestoryAreaEntity();
                    destoryAreaEntity.setStructureType(FxfzConstants.GJHNT_STRUCTURE);
                    destoryAreaEntity.setJbArea(destoryRateEntity.getZero().multiply(fourArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setQwArea(destoryRateEntity.getOne().multiply(fourArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setZdArea(destoryRateEntity.getTwo().multiply(fourArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setYzArea(destoryRateEntity.getThree().multiply(fourArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setHhArea(destoryRateEntity.getFour().multiply(fourArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntityList.add(destoryAreaEntity);
                }
                if (destoryRateEntity.getStructureType().equals(FxfzConstants.MUZHU_STRUCTURE)) {
                    DestoryAreaEntity destoryAreaEntity = new DestoryAreaEntity();
                    destoryAreaEntity.setStructureType(FxfzConstants.MUZHU_STRUCTURE);
                    destoryAreaEntity.setJbArea(destoryRateEntity.getZero().multiply(fiveArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setQwArea(destoryRateEntity.getOne().multiply(fiveArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setZdArea(destoryRateEntity.getTwo().multiply(fiveArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setYzArea(destoryRateEntity.getThree().multiply(fiveArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setHhArea(destoryRateEntity.getFour().multiply(fiveArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntityList.add(destoryAreaEntity);
                }
                if (destoryRateEntity.getStructureType().equals(FxfzConstants.HZ_STRUCTURE)) {
                    DestoryAreaEntity destoryAreaEntity = new DestoryAreaEntity();
                    destoryAreaEntity.setStructureType(FxfzConstants.HZ_STRUCTURE);
                    destoryAreaEntity.setJbArea(destoryRateEntity.getZero().multiply(sixArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setQwArea(destoryRateEntity.getOne().multiply(sixArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setZdArea(destoryRateEntity.getTwo().multiply(sixArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setYzArea(destoryRateEntity.getThree().multiply(sixArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setHhArea(destoryRateEntity.getFour().multiply(sixArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntityList.add(destoryAreaEntity);
                }
                if (destoryRateEntity.getStructureType().equals(FxfzConstants.GANG_STRUCTURE)) {
                    DestoryAreaEntity destoryAreaEntity = new DestoryAreaEntity();
                    destoryAreaEntity.setStructureType(FxfzConstants.GANG_STRUCTURE);
                    destoryAreaEntity.setJbArea(destoryRateEntity.getZero().multiply(sevenArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setQwArea(destoryRateEntity.getOne().multiply(sevenArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setZdArea(destoryRateEntity.getTwo().multiply(sevenArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setYzArea(destoryRateEntity.getThree().multiply(sevenArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setHhArea(destoryRateEntity.getFour().multiply(sevenArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntityList.add(destoryAreaEntity);
                }
                if (destoryRateEntity.getStructureType().equals(FxfzConstants.OTHER_STRUCTURE)) {
                    DestoryAreaEntity destoryAreaEntity = new DestoryAreaEntity();
                    destoryAreaEntity.setStructureType(FxfzConstants.OTHER_STRUCTURE);
                    System.out.println("one" + destoryRateEntity.getOne());
                    System.out.println("two" + destoryRateEntity.getTwo());
                    destoryAreaEntity.setJbArea(destoryRateEntity.getZero().multiply(eightArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setQwArea(destoryRateEntity.getOne().multiply(eightArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setZdArea(destoryRateEntity.getTwo().multiply(eightArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setYzArea(destoryRateEntity.getThree().multiply(eightArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setHhArea(destoryRateEntity.getFour().multiply(eightArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntityList.add(destoryAreaEntity);
                }
                if (destoryRateEntity.getStructureType().equals(FxfzConstants.KZQQT_STRUCTURE)) {
                    DestoryAreaEntity destoryAreaEntity = new DestoryAreaEntity();
                    destoryAreaEntity.setStructureType(FxfzConstants.KZQQT_STRUCTURE);
                    destoryAreaEntity.setJbArea(destoryRateEntity.getZero().multiply(nineArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setQwArea(destoryRateEntity.getOne().multiply(nineArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setZdArea(destoryRateEntity.getTwo().multiply(nineArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setYzArea(destoryRateEntity.getThree().multiply(nineArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntity.setHhArea(destoryRateEntity.getFour().multiply(nineArea).setScale(6, RoundingMode.HALF_UP));
                    destoryAreaEntityList.add(destoryAreaEntity);
                }
            }


            //F（i）的值
            FOneEntity fOneEntity = new FOneEntity();
            BigDecimal jbFone = new BigDecimal(0);
            BigDecimal qwFone = new BigDecimal(0);
            BigDecimal zdFone = new BigDecimal(0);
            BigDecimal yzFone = new BigDecimal(0);
            BigDecimal hhFone = new BigDecimal(0);
            for (DestoryAreaEntity destoryAreaEntity : destoryAreaEntityList) {
                jbFone = jbFone.add(destoryAreaEntity.getJbArea().setScale(6, RoundingMode.HALF_UP));
                qwFone = qwFone.add(destoryAreaEntity.getQwArea().setScale(6, RoundingMode.HALF_UP));
                zdFone = zdFone.add(destoryAreaEntity.getZdArea().setScale(6, RoundingMode.HALF_UP));
                yzFone = yzFone.add(destoryAreaEntity.getYzArea().setScale(6, RoundingMode.HALF_UP));
                hhFone = hhFone.add(destoryAreaEntity.getHhArea().setScale(6, RoundingMode.HALF_UP));
            }

            //F（i）的实体
            fOneEntity.setJbFone(jbFone);
            fOneEntity.setQwFone(qwFone);
            fOneEntity.setZdFone(zdFone);
            fOneEntity.setYzFone(yzFone);
            fOneEntity.setHhFone(hhFone);

            //总面积
            BigDecimal zongArea = zeroArea.add(oneArea).add(twoArea).add(threeArea).add(fourArea.add(fiveArea.add(sixArea.add(sevenArea.add(eightArea.add(nineArea)))))).setScale(6, RoundingMode.HALF_UP);


            //A的值
            AEntity aEntity = new AEntity();
            aEntity.setJbA(fOneEntity.getJbFone().divide(zongArea, 6, RoundingMode.HALF_UP));
            aEntity.setZdA(fOneEntity.getZdFone().divide(zongArea, 6, RoundingMode.HALF_UP));
            aEntity.setQwA(fOneEntity.getQwFone().divide(zongArea, 6, RoundingMode.HALF_UP));
            aEntity.setYzA(fOneEntity.getYzFone().divide(zongArea, 6, RoundingMode.HALF_UP));
            aEntity.setHhA(fOneEntity.getHhFone().divide(zongArea, 6, RoundingMode.HALF_UP));


            String done = algVO.getDone();
            BigDecimal d1 = new BigDecimal(done);

            String eone = algVO.getEone();
            BigDecimal e1 = new BigDecimal(eone);

            //RD 的值
            RdEntity rdEntity = new RdEntity();
            //常数 10  以10为低的 10   用户的log 是以10 为底的；
            Double cTen = new Double(10.00);
            rdEntity.setJbRd(BigDecimal.valueOf(Math.pow(cTen, d1.doubleValue() * Math.pow(aEntity.getJbA().doubleValue(), 0.1) - e1.doubleValue())).setScale(10, RoundingMode.HALF_UP));
            rdEntity.setQwRd(BigDecimal.valueOf(Math.pow(cTen, d1.doubleValue() * Math.pow(aEntity.getQwA().doubleValue(), 0.1) - e1.doubleValue())).setScale(10, RoundingMode.HALF_UP));
            rdEntity.setZdRd(BigDecimal.valueOf(Math.pow(cTen, d1.doubleValue() * Math.pow(aEntity.getZdA().doubleValue(), 0.1) - e1.doubleValue())).setScale(10, RoundingMode.HALF_UP));
            rdEntity.setYzRd(BigDecimal.valueOf(Math.pow(cTen, d1.doubleValue() * Math.pow(aEntity.getYzA().doubleValue(), 0.1) - e1.doubleValue())).setScale(10, RoundingMode.HALF_UP));
            rdEntity.setHhRd(BigDecimal.valueOf(Math.pow(cTen, d1.doubleValue() * Math.pow(aEntity.getHhA().doubleValue(), 0.1) - e1.doubleValue())).setScale(10, RoundingMode.HALF_UP));

            // 调用基础数据中的 人口普查数据
            //查询JcPopulation 中的区的对象p
            Map<String, Object> paramMap1 = new HashMap<>();
            paramMap1.put("province", districtEntity.getProvince());
            paramMap1.put("city", districtEntity.getCity());
            paramMap1.put("county", districtEntity.getPrefecture());
            paramMap1.put("dataVersion", algVO.getRkpcDataVersion());

            ResponseEntity<String> poResponseEntity = restTemplateUtil.sendHttpRequestPj(poUrl, paramMap1);
            JcPopulation jcPopulation = JSONObject.parseObject(poResponseEntity.getBody(), JcPopulation.class);
            if (PlatformObjectUtils.isEmpty(jcPopulation)) {
                return RestResponse.fail("所选省市区范围内，未查询到人口普查数据，请添加人口普查数据");
            }

            //获取一个区的死亡人数
            Integer deathNum = (int) (jcPopulation.getTotalPeople() * rdEntity.getHhRd().doubleValue() * Integer.parseInt(algVO.getSi()));
            //受伤人数
            Integer injuryNum = deathNum * algVO.getCc();
            //保存到数据库
            //根据省市区 获取省市区的空间字段

            String district1 = null;
            if ("北京市".equals(districtEntity.getProvince()) || "上海市".equals(districtEntity.getProvince()) || "天津市".equals(districtEntity.getProvince()) || "重庆市".equals(districtEntity.getProvince())) {
                district1 = districtEntity.getCity() + "-" + districtEntity.getPrefecture();
            } else {
                district1 = districtEntity.getProvince() + "-" + districtEntity.getCity() + "-" + districtEntity.getPrefecture();
            }
            String code = orgDivisionRepository.getCodeByTaskDistrict(district1);

            String geom = destoryAlgRepository.getGeomBySSQ(code);
            AlgEntity algEntity = new AlgEntity();
            algEntity.setNumber("SW" + sbf.format(date));
            algEntity.setResultAnalysis(uuid);
            algEntity.setId(UUIDGenerator.getUUID());
            algEntity.setProvince(districtEntity.getProvince());
            algEntity.setCity(districtEntity.getCity());
            algEntity.setPrefecture(districtEntity.getPrefecture());
            algEntity.setDeathNum(deathNum);
            algEntity.setInjuryNum(injuryNum);
            algEntity.setIaId(algVO.getIaId());
            algEntity.setGeom(geom);
            algEntity.setDeathXznum(deathNum);
            algEntity.setInjuryXznum(injuryNum);
            if (algEntity.getDeathXznum() < 10) {
                algEntity.setLevell("5");
            } else if (algEntity.getDeathXznum() >= 10 && algEntity.getDeathXznum() < 50) {
                algEntity.setLevell("4");
            } else if (algEntity.getDeathXznum() >= 50 && algEntity.getDeathXznum() < 150) {
                algEntity.setLevell("3");
            } else if (algEntity.getDeathXznum() >= 150 && algEntity.getDeathXznum() < 300) {
                algEntity.setLevell("2");
            } else if (algEntity.getDeathXznum() >= 300) {
                algEntity.setLevell("1");
            }
            algEntity.setDeathAndInjuryNum(deathNum+injuryNum);
            destoryAlgRepository.save(algEntity);
            //计算需紧急安置人数所需数据
            MedianEntity medianEntity = new MedianEntity();
            medianEntity.setProvince(districtEntity.getProvince());
            medianEntity.setCity(districtEntity.getCity());
            medianEntity.setPrefecture(districtEntity.getPrefecture());
            medianEntity.setDeathNum(BigDecimal.valueOf(deathNum));
            medianEntity.setList(destoryAreaEntityList);
            medianEntity.setId(algEntity.getId());
            medianEntity.setPerCapitaHouseArea(BigDecimal.valueOf(jcPopulation.getPerCapitaHouseArea()));
            medianEntities.add(medianEntity);
        }

        countPopulationService.count(medianEntities);


        List<AlgEntity> alg = getList(uuid);
        return RestResponse.succeed(alg);

    }

    private List<AlgEntity> getList(String uuid) {
        List<AlgEntity> algEntityList = destoryAlgRepository.getList(uuid);
        return algEntityList;
    }

    @Override
    public List<AlgEntity> getAlg(AlgVO algVO) {
        List<AlgEntity> algEntityList = new ArrayList<>();
        for (DistrictEntity districtEntity : algVO.getList()) {
            AlgEntity algEntity = destoryAlgRepository.getAlg(districtEntity.getProvince(), districtEntity.getCity(), districtEntity.getPrefecture(), algVO);
            if (algEntity != null) {
                algEntityList.add(algEntity);
            }

        }

        return algEntityList;
    }

    @Override
    public void updateAlg(List<AlgEntity> algEntityList) {
        for (AlgEntity algEntity : algEntityList) {
            if (algEntity.getDeathXznum() < 10) {
                algEntity.setLevell("5");
            } else if (algEntity.getDeathXznum() >= 10 && algEntity.getDeathXznum() < 50) {
                algEntity.setLevell("4");
            } else if (algEntity.getDeathXznum() >= 50 && algEntity.getDeathXznum() < 150) {
                algEntity.setLevell("3");
            } else if (algEntity.getDeathXznum() >= 150 && algEntity.getDeathXznum() < 300) {
                algEntity.setLevell("2");
            } else if (algEntity.getDeathXznum() >= 300) {
                algEntity.setLevell("1");
            }
            destoryAlgRepository.updateAlg(algEntity);
        }

    }

    @Override
    public List<AlgEntity> getAlgByIaId(String iaId) {
        return destoryAlgRepository.getAlgByIaId(iaId);
    }


}
